package top.wangjiu.java.jvm.attribute;

import top.wangjiu.java.jvm.AttributeInfo;
import top.wangjiu.java.jvm.attribute.info.LocalVariableInfo;
import top.wangjiu.java.jvm.constantinfo.ConstantPoolInfo;

public class LocalVariableTableAttr extends AttributeInfo {

    public final static String NAME = "LocalVariableTable";

    private final short attributeNameIndex;   //属性名索引
    private final int attributeLength;
    //字段值在常量池中的索引，常量池在该索引处的项给出该属性表示的常量值。（例如，值是long型的，在常量池中便是CONSTANT_Long）
    private final short localVariableTableLength;
    private final LocalVariableInfo[] localVariableTable;

    public LocalVariableTableAttr(short attributeNameIndex,int attributeLength, LocalVariableInfo[] localVariableTable){
        this.attributeNameIndex = attributeNameIndex;
        this.attributeLength = attributeLength;
        this.localVariableTableLength = (short) localVariableTable.length;
        this.localVariableTable = localVariableTable;
    }

    @Override
    public short attributeNameIndex() {
        return attributeNameIndex;
    }

    @Override
    public int attributeLength() {
        return attributeLength;
    }

    @Override
    public String toString(ConstantPoolInfo[] poolInfos) {
        StringBuilder sb = new StringBuilder("LocalVariableTable:");
        for (int i = 0; i < localVariableTableLength; i++) {
            sb.append(" "+localVariableTable[i].toString(poolInfos));
        }
        return sb.toString();
    }
}
